package com.laikan.reader.bookshelf.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.laikan.reader.R;
import com.laikan.reader.bookshelf.activity.BookShelfActivity;
import com.laikan.reader.bookshelf.adapter.holder.AddHolder;
import com.laikan.reader.bookshelf.adapter.holder.BookShelfGridHolder;
import com.laikan.reader.database.Book;
import com.socks.library.KLog;

import java.util.HashSet;
import java.util.List;

/**
 * Created by Administrator on 2016/8/22.
 */
public class BookShelfRecyclerViewAdapter extends RecyclerView.Adapter implements View.OnClickListener, View.OnLongClickListener {

    private static int ITEM_TYPE_BOOKFACE = 0;
    private static int ITEM_TYPE_ADDBOOK = 1;

    private OnItemLongClickListener onItemLongClickListener = null;
    private OnItemClickListener onItemClickListener = null;

    private List<Book> mBookItemList;
    private HashSet mDelelteBookHashSet;

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private boolean mIsDeleteManager = false;

    private int mBookItemListCount;

    public BookShelfRecyclerViewAdapter(Context context, List<Book> datas) {
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mContext = context;
        mBookItemList = datas;
        if (mIsDeleteManager) {
            mBookItemListCount = mBookItemList.size();
        }else {
            mBookItemListCount = mBookItemList.size() + 1;
        }
    }

    @Override
    public int getItemCount() {
        return mBookItemListCount;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO: 2016/9/1  这种判断item类型的方式不优化
        if (mBookItemListCount - 1 == position && mIsDeleteManager == false) {
            return ITEM_TYPE_ADDBOOK;
        }else {
            return ITEM_TYPE_BOOKFACE;
        }
    }

    public void upDatas(List<Book> datas, HashSet map) {
        this.mDelelteBookHashSet = map == null ? new HashSet<String>() : map;
        this.mBookItemList.clear();
        this.mBookItemList.addAll(datas);
        if (mIsDeleteManager) {
            mBookItemListCount = mBookItemList.size();
        }else {
            mBookItemListCount = mBookItemList.size() + 1;
        }
        this.notifyDataSetChanged();
    }

    /**
     * 用于控制父Activity中ViewPager滑动，和设置书架和删除页状态
     *
     * @param press
     * @param main
     */
    public void setLongPressStatus(boolean press, BookShelfActivity main) {
        this.mIsDeleteManager = press;
        if (mIsDeleteManager) {
            mBookItemListCount = mBookItemList.size();
        }else {
            mBookItemListCount = mBookItemList.size() + 1;
        }
    }

    private boolean isDeleteManager() {
        return mIsDeleteManager;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        View view;

        switch (viewType) {
            case 0:
                view = mLayoutInflater.inflate(R.layout.books_grid_item, parent, false);
                view.setOnClickListener(this);
                view.setOnLongClickListener(this);
                viewHolder = new BookShelfGridHolder(view);
                break;
            case 1:
                view = mLayoutInflater.inflate(R.layout.books_add_item, parent, false);
                view.setOnClickListener(this);
                view.setOnLongClickListener(this);
                viewHolder = new AddHolder(view);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BookShelfGridHolder) {
            Book bean = mBookItemList.get(position);
            if (bean != null) {
                KLog.e("adapter onBindViewHolder: " + " name: " + bean.getName() + " sortIndex: " + bean.getSortIndex() + " position: " + position);
                ((BookShelfGridHolder)holder).getRootView().setTag(bean);
                ((BookShelfGridHolder)holder).setData(bean);
                if (isDeleteManager()) {
                    ((BookShelfGridHolder)holder).setDeleteManagerStatus(true);
                    if (mDelelteBookHashSet.contains(bean.getId())) {
                        ((BookShelfGridHolder)holder).setSelectStatus(true);
                    }else {
                        ((BookShelfGridHolder)holder).setSelectStatus(false);
                    }
                }else {
                    ((BookShelfGridHolder)holder).setSelectStatus(false);
                    ((BookShelfGridHolder)holder).setDeleteManagerStatus(false);
                }
                ((BookShelfGridHolder)holder).refreshViewSelectStatus();
            }
        }else if (holder instanceof AddHolder) {
            //暂时不用绑定
            ((AddHolder)holder).getRootView().setTag("add");
        }
    }

    @Override
    public void onClick(View view) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            onItemClickListener.onItemClick(view, view.getTag());
        }
    }

    @Override
    public boolean onLongClick(View view) {
        return onItemLongClickListener != null && onItemLongClickListener.onLongClick(view, view.getTag());
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.onItemLongClickListener = listener;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, Object data);
    }

    public interface OnItemLongClickListener {

        boolean onLongClick(View view, Object data);
    }
}
